<?php

namespace App\Http\Controllers;

use App\Lianxi;
use App\Test1130;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class Test1130Controller extends Controller
{
    /**
     * 添加页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(){
        $data = Test1130::getA()->toArray();
        return view('test1130add',['data'=>$data]);
    }

    /**
     * 添加方法
     * @param Request $request
     * @return string
     */
    public function adddo(Request $request){
        $validator = Validator::make($request->all(), [
            'f_name' => 'required',
        ],[
            'f_name.required' => '房源名称必须填写！',
            'f_name.unique'  => '房源名称重复！',
        ]);

        if ($validator->fails()) {
            return $validator->errors()->first();
        }
        $data['f_name'] = $request['f_name'];
        $data['f_pic'] = $request['f_pic'];
        $data['f_word'] = $request['f_word'];
        $data['p_id'] = $request['p_id'];
        $res = Test1130::addOne($data);
        if($res){
            echo "<script>alert('添加成功')</script>";
            header('refresh:0;url=show');
        }
    }

    /**
     * 异步获取图片的方法
     * @param Request $request
     * @return array
     */
    public function getImg(Request $request){
        $path = $request->file('file')->store('images');
        if($path){
            return ['data'=>$path,'code'=>0,'msg'=>'上传成功'];
        }else{
            return ['data'=>"",'code'=>1,'msg'=>'上传失败'];
        }
    }

    /**
     * 展示页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     *
     */
    public function show(){
        $data1 = Test1130::getA()->toArray();
        $data = Lianxi::getTree1($data1);
        return view('test1130show',['data'=>$data]);
    }

    /**
     * 软删除方法
     * @param Request $request
     * @return array
     */
    public function del(Request $request){
        $where['f_id'] = $request['id'];
        $data1 = Test1130::getOne($where)->toArray()[0];
        if($data1['p_id'] == 0){
            return ['data' => "" , 'code' => 1 , 'msg' => '有子集的不能删除!'];
        }
        if($data1['deleted_at'] == "1"){
            $data['deleted_at'] = "2";
            $res = Test1130::delUp($where,$data);
            if($res){
                return ['data' => $data1 , 'code' => 0 , 'msg' => '下架成功!'];
            }else{
                return ['data' => "" , 'code' => 1 , 'msg' => '下架失败!'];
            }
        }else{
            $data['deleted_at'] = "1";
            $res = Test1130::delUp($where,$data);
            if($res){
                return ['data' => $data1 , 'code' => 0 , 'msg' => '发布成功!'];
            }else{
                return ['data' => "" , 'code' => 1 , 'msg' => '发布失败!'];
            }
        }
    }

    /**
     * 修改页面
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public  function up(Request $request){
        $where['f_id'] = $request['f_id'];
        $arr = Test1130::getOne($where)->toArray()[0];
        $data = Test1130::getA()->toArray();
        return view('test1130up',['data'=>$data,'arr'=>$arr,'where'=>$where['f_id']]);
    }

    /**
     * 修改方法
     * @param Request $request
     */
    public function updo(Request $request){
        $where['f_id'] = $request['f_id'];
        $data['f_name'] = $request['f_name'];
        $data['f_pic'] = $request['f_pic'];
        $data['f_word'] = $request['f_word'];
        $data['p_id'] = $request['p_id'];
        $res = Test1130::delUp($where,$data);
        if($res){
            echo "<script>alert('修改成功')</script>";
            header('refresh:0;url=show');
        }else{
            echo "修改失败";
        }
    }
}
